﻿using Framework.AspNetCore.Mvc;
using Framework.AspNetCore.UnifyResult.Fiters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;

namespace Framework.AspNetCore.UnifyResult
{
	/// <summary>
	/// 规范化接口
	/// 由于太多人反应，想兼容一套类似furion的返回情况，200状态码包一层更符合国内习惯，既然如此，不如直接搬过来
	/// </summary>
	public static class UnifyResultExtensions
	{
		public static IServiceCollection AddFurionUnifyResultApi(this IServiceCollection services)
		{
			//成功规范接口
			services.AddTransient<SucceededUnifyResultFilter>();
			//异常规范接口
			services.AddTransient<FriendlyExceptionFilter>();
			services.AddMvc(options =>
			{
				options.Filters.AddService<SucceededUnifyResultFilter>(99);
				options.Filters.AddService<FriendlyExceptionFilter>(100);
				options.Filters.RemoveAll(x => (x as ServiceFilterAttribute)?.ServiceType == typeof(AbpExceptionFilter));

				// 添加应用模型转换器
				options.Conventions.Add(new ApiControllerApplicationModelConvention(services));
			});

			return services;
		}
	}
}
